{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2acfe349",
   "metadata": {},
   "source": [
    "----\n",
    "* 本周主要内容：语音识别+自然语言处理实践（对话机器人实现原理）\n",
    "* 23春_API_人工智能与机器学习_week12\n",
    "* 电子讲义设计者：许智超\n",
    "\n",
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a710dc1",
   "metadata": {},
   "source": [
    "# 1. 对话机器人实现原理\n",
    "\n",
    "> 1. 识别语音-->转换百度语音识别指定格式文件\n",
    "> 1. 调用百度asr接口，转换成文本\n",
    "> 1. 回复？数据结构：字典 {key:value} , key（人） 作为输入 Input, value（机器） 作为 Output\n",
    "> 1. {\"你好呀\":\"你也好呀，看起来心情不错呀！\"}\n",
    "> 1. 回复value的信息\n",
    "> 1. 接语音合成，完成智能语音对话机器人"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dccee84c",
   "metadata": {},
   "source": [
    "# 2. 识别语音(百度API)测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82158ad3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "from urllib.request import urlopen\n",
    "from urllib.request import Request\n",
    "from urllib.error import URLError\n",
    "from urllib.parse import urlencode\n",
    "timer = time.perf_counter\n",
    "\n",
    "\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = 'speech-demo-master/rest-api-asr/python/audio/16k.pcm'  # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "\"----------------------------------------------------------------------------\"\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "# 获取access_token\n",
    "def fetch_token():\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "#         print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "#     print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "#     print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if SCOPE and (not SCOPE in result['scope'].split(' ')):  # SCOPE = False 忽略检查\n",
    "            raise DemoError('scope is not correct')\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "# 语音识别接口的调用\n",
    "\"----------------------------------------------------------------------------\"\n",
    "token = fetch_token()\n",
    "speech_data = []\n",
    "# 打开目标语音文件\n",
    "with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "    speech_data = speech_file.read()\n",
    "length = len(speech_data)\n",
    "if length == 0:\n",
    "    raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "params = {\n",
    "    'cuid': CUID, \n",
    "    'token': token, \n",
    "    'dev_pid': DEV_PID\n",
    "}\n",
    "#测试自训练平台需要打开以下信息\n",
    "#params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "params_query = urlencode(params);\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "    'Content-Length': length\n",
    "}\n",
    "\n",
    "url = ASR_URL + \"?\" + params_query\n",
    "\n",
    "# print post_data\n",
    "req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "# 异常处理，避免出现红色的bug（提前预判可能出现的异常）\n",
    "try:\n",
    "    begin = timer()\n",
    "    f = urlopen(req)\n",
    "    result_str = f.read()\n",
    "#     print(\"Request time cost %f\" % (timer() - begin))\n",
    "except  URLError as err:\n",
    "    print('asr http response http code : ' + str(err.code))\n",
    "    result_str = err.read()\n",
    "\n",
    "if (IS_PY3):\n",
    "    result_str = str(result_str, 'utf-8')\n",
    "print(\"结果：\",result_str)\n",
    "# 文本转字典\n",
    "result_str = eval(result_str)\n",
    "result_整理 = result_str['result'][0].split('。')[0]\n",
    "result_整理\n",
    "# with open(\"result.txt\", \"w\") as of:\n",
    "#     of.write(result_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0249bafd",
   "metadata": {},
   "source": [
    "# 3. 回复字典创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56f3a36d",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"北京科技馆\":\"中国科学技术馆，位于北京市朝阳区北辰东路5号，东临亚运居住区，西濒奥运水系，南依奥运主体育场，北望森林公园，于1958年筹建，建设分为两期工程和新馆工程，分别于1988年9月22日、2000年4月29日、2009年9月16日开放。\",\n",
    "    \"你好呀\":\"你也好呀，看起来心情不错呀！\",\n",
    "    \"你叫什么\":\"我是集美貌与才华一身的小度小度呀\",\n",
    "    \"设置闹钟\":\"您想要设置几点的闹钟呢？现在已经晚上12点了，早点休息呀\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "666db77b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试字典\n",
    "qa.get(\"北京科技馆\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1043b15",
   "metadata": {},
   "source": [
    "# 4. 结合 2，3 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75a2bd68",
   "metadata": {},
   "outputs": [],
   "source": [
    "result_整理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a638903f",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa.get(result_整理)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b9343f7",
   "metadata": {},
   "source": [
    "# 5. 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8210e79f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "import sys\n",
    "import json\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "    from urllib.parse import quote_plus\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib import quote_plus\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "\n",
    "API_KEY = 'smxkOHWjqLVljEmIry5vuSYI'\n",
    "SECRET_KEY = 'ky3FZeSSDurxLyLLqZ2kPaEnts9NH1W1'\n",
    "\n",
    "TEXT = qa.get(result_整理)\n",
    "\n",
    "# 发音人选择, 基础音库：0为度小美，1为度小宇，3为度逍遥，4为度丫丫，\n",
    "# 精品音库：5为度小娇，103为度米朵，106为度博文，110为度小童，111为度小萌，默认为度小美 \n",
    "PER = 4\n",
    "# 语速，取值0-15，默认为5中语速\n",
    "SPD = 5\n",
    "# 音调，取值0-15，默认为5中语调\n",
    "PIT = 5\n",
    "# 音量，取值0-9，默认为5中音量\n",
    "VOL = 5\n",
    "# 下载的文件格式, 3：mp3(default) 4： pcm-16k 5： pcm-8k 6. wav\n",
    "AUE = 6\n",
    "\n",
    "FORMATS = {3: \"mp3\", 4: \"pcm\", 5: \"pcm\", 6: \"wav\"}\n",
    "FORMAT = FORMATS[AUE]\n",
    "\n",
    "CUID = \"123456PYTHON\"\n",
    "\n",
    "TTS_URL = 'http://tsn.baidu.com/text2audio'\n",
    "\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "SCOPE = 'audio_tts_post'  # 有此scope表示有tts能力，没有请在网页里勾选\n",
    "\n",
    "\n",
    "def fetch_token():\n",
    "    print(\"fetch token begin\")\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req, timeout=5)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "    print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "    print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if not SCOPE in result['scope'].split(' '):\n",
    "            raise DemoError('scope is not correct')\n",
    "        print('SUCCESS WITH TOKEN: %s ; EXPIRES IN SECONDS: %s' % (result['access_token'], result['expires_in']))\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    token = fetch_token()\n",
    "    tex = quote_plus(TEXT)  # 此处TEXT需要两次urlencode\n",
    "    print(tex)\n",
    "    params = {'tok': token, 'tex': tex, 'per': PER, 'spd': SPD, 'pit': PIT, 'vol': VOL, 'aue': AUE, 'cuid': CUID,\n",
    "              'lan': 'zh', 'ctp': 1}  # lan ctp 固定参数\n",
    "\n",
    "    data = urlencode(params)\n",
    "    print('test on Web Browser' + TTS_URL + '?' + data)\n",
    "\n",
    "    req = Request(TTS_URL, data.encode('utf-8'))\n",
    "    has_error = False\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "\n",
    "        headers = dict((name.lower(), value) for name, value in f.headers.items())\n",
    "\n",
    "        has_error = ('content-type' not in headers.keys() or headers['content-type'].find('audio/') < 0)\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "        has_error = True\n",
    "\n",
    "    save_file = \"error.txt\" if has_error else 'result.' + FORMAT\n",
    "    with open(save_file, 'wb') as of:\n",
    "        of.write(result_str)\n",
    "\n",
    "    if has_error:\n",
    "        if (IS_PY3):\n",
    "            result_str = str(result_str, 'utf-8')\n",
    "        print(\"tts api  error:\" + result_str)\n",
    "\n",
    "    print(\"result saved as :\" + save_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f395d7d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pydub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af7ef880",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f43a427",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install pipwin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6a4a3b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59b44392",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f19f9f09",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cdd8daf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "656px",
    "left": "67px",
    "top": "0px",
    "width": "281.398px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
